function [graphPrices, graphYMat ] = plotAggCostStep(ctrS)

numPriceVals = 500;
priceMin = 1;
priceMax = 50;

ctrS.estim.bids.index = (1:height(ctrS.estim.bids))';
one_row_per_pp = groupfilter(ctrS.estim.bids, "id_pp", @(x) x == min(x), "index");
one_row_per_pp = one_row_per_pp(:, ["id_pp", "id_plant", "id_period"]);
sparePriceTable = table();
priceLevels = linspace(priceMin, priceMax, numPriceVals)';

for i = 1:height(one_row_per_pp)
    % Duplicate the current row
    duplicatedRow = repmat(one_row_per_pp(i, :), numPriceVals, 1);

    duplicatedRow.price = priceLevels;

    % Append the duplicated row to the new table
    sparePriceTable = [sparePriceTable; duplicatedRow];
end

sparePriceTable = sparePriceTable(sparePriceTable.id_period==ctrS.period,["id_pp", "id_period", "id_plant", "price"]);

graphTableMerged = outerjoin(ctrS.output_it,sparePriceTable, ...
    'Keys',{'id_plant', 'id_period'},'MergeKeys',true, 'Type','right');

graphTableMerged = removevars(graphTableMerged, ["id_period","id_plant"]);


graphTableMerged.Emissions(graphTableMerged.price>=graphTableMerged.pp_avg_p) = ...
    graphTableMerged.E_abate(graphTableMerged.price>=graphTableMerged.pp_avg_p) ;
graphTableMerged.Emissions(graphTableMerged.price<graphTableMerged.pp_avg_p) = ...
    graphTableMerged.E_noabate(graphTableMerged.price<graphTableMerged.pp_avg_p) ;

graphTableMerged2 = graphTableMerged(:,["Emissions", "id_pp", "price"]);

graphTableUnstack = unstack(graphTableMerged2,"Emissions","id_pp") ;

graphTableUnstackY = removevars(graphTableUnstack, "price");

graphYMat = table2array(graphTableUnstackY);
graphPrices = graphTableUnstack.price;

end